package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.List;

/**
 * spring容器启动监听器, 拥有初始化redis数据
 */


//@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent>{


    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    //spring容器启动之后马山执行方法
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("------------vo数据初始化-start--------------------------");
        //从mysql中获取所有的攻略明细数据
        List<StrategyDetail> list = strategyDetailService.list();
        StrategyStatisVO vo = null;
       /* for (StrategyDetail detail : list) {
            //存在一个小细节: 如果redis已经存在该vo数据, 需要跳过, 否则会出现数据丢失情况
            if(strategyStatisRedisService.isStrategyStatisVOExsit(detail.getId())){
                continue;  //跳过此次循环
            }
            vo = new StrategyStatisVO();
            //遍历添加到redis中
            try {
                //强调: 属性名一致时,才可以进行copy
                BeanUtils.copyProperties(vo, detail);//底层使用javabean内省机制
                vo.setDestId(detail.getDest().getId());
                vo.setDestName(detail.getDest().getName());
                vo.setStrategyId(detail.getId());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            //设置
            strategyStatisRedisService.setStrategyStatisVO(vo);
        }*/

        System.out.println("------------end--------------------------");
        //



        System.out.println("------------zset 数据初始化--------------------------");

        for (StrategyDetail detail : list){
            //模拟zset数据初始化, 所有攻略明细分数都是0

            //为了防止重复启动mgrsite项目,多次执行初始化话覆盖redis中排序数据, 需要进行排除
            //如果zset数据已经存在,直接跳过

            String value = RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString());
            //参数1: zset的key   参数2:zset 的value
            if(strategyStatisRedisService.isZsetHasValue(RedisKeys.STRATEGY_STATIS_COMMEND_SORT.getPrefix(), value)){
               continue;
            }

            //点赞数+收藏数
            int score = detail.getThumbsupnum() + detail.getFavornum();
            strategyStatisRedisService.addCommendScores(value, score);
        }

        for (StrategyDetail detail : list){

            String value = RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString());
            //参数1: zset的key   参数2:zset 的value
            if(strategyStatisRedisService.isZsetHasValue(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefix(), value)){
                continue;
            }

            //取浏览数+评论数
            int score = detail.getViewnum() + detail.getReplynum();
            strategyStatisRedisService.addHotScores(value, score);

        }

        System.out.println("------------zset 数据初始化end--------------------------");


    }
}
